//
//  ASAvatarGroupSearchView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/29.
//

#import "ASAvatarGroupSearchView.h"

@implementation ASAvatarGroupSearchView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        [self createAllData];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createAllData];
    }
    return self;
}

- (void)createAllData{
    [self.contentView addSubview:self.textBackgroundView];
    [self.contentView addSubview:self.searchBtn];
    [self.contentView addSubview:self.searchText];
    
    @weakify(self);
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.right.mas_equalTo(-14);
        make.size.mas_equalTo(CGSizeMake(50, 28));
    }];
    
    [self.textBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.left.mas_equalTo(14);
        make.right.mas_equalTo(-14);
        make.height.mas_equalTo(28);
    }];
    
    [self.searchText mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.left.mas_equalTo(14);
        make.right.mas_equalTo(self.searchBtn.mas_left);
        make.height.mas_equalTo(28);
    }];
}

- (UITextField *)searchText{
    if (!_searchText) {
        _searchText = [UITextField new];
        _searchText.textColor = [UIColor colorWithHexString:@"1a1a1a"];
        _searchText.font= [UIFont systemFontOfSize:14];
//        _searchText.clearButtonMode = UITextFieldViewModeWhileEditing;                                     //只有编辑时出现出现那个叉叉
        _searchText.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];
        _searchText.delegate = self;
        _searchText.returnKeyType = UIReturnKeySearch;
        _searchText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入要搜索的内容"attributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#999797"]}];
        _searchText.layer.masksToBounds = YES;
        _searchText.layer.cornerRadius = 14;
        [_searchText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

        UIView *leftV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        UIImageView *leftImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_ss"]];
        leftImage.frame = CGRectMake(10, 8, 16, 15);
        [leftV addSubview:leftImage];
        _searchText.leftView = leftV;
        _searchText.leftViewMode = UITextFieldViewModeAlways;
//        _searchText.rightView = self.searchBtn;
//        _searchText.rightViewMode = UITextFieldViewModeAlways;
    }
    return _searchText;
}

- (UIView *)textBackgroundView{
    if (!_textBackgroundView) {
        _textBackgroundView = [UIView new];
        _textBackgroundView.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];
        _textBackgroundView.layer.masksToBounds = YES;
        _textBackgroundView.layer.cornerRadius = 14;
    }
    return _textBackgroundView;
}

- (UIButton *)searchBtn{
    if (!_searchBtn) {
        _searchBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 28)];
        [_searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        [_searchBtn setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
        _searchBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_searchBtn setBackgroundColor:[UIColor colorWithHexString:@"#FC6783"]];
        _searchBtn.layer.masksToBounds = YES;
        _searchBtn.layer.cornerRadius = 14;
        [_searchBtn addTarget:self action:@selector(clickSearchBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    [self clickSearchBtn];
    return YES;
}

- (void)clickSearchBtn{
    [self.searchText resignFirstResponder];
    if (self.clickSearchBlock) {
        self.clickSearchBlock(self.searchText.text);
    }
}

- (void)textFieldDidChange:(UITextField *)textField{
   if (textField.markedTextRange == nil) {
       NSLog(@"dsafadfadsfadsfdsafasdfsadfadsfwwwwwwwwwwwww:%@", textField.text);
       if (self.changeSearchContentBlock) {
           self.changeSearchContentBlock(textField.text);
       }
   }
}
@end
